##Authors and contact: Naielly Lopes Marques (naielly.lopes@iag.puc-rio.br), Carlos de Lamare Bastian-Pinto (carbastian@gmail.com), and Luiz Eduardo Teixeira Brand�o (brandao@iag.puc-rio.br). Institution: Pontifical Catholic University of Rio de Janeiro (PUC-RJ). Department: IAG Business School
##Purpose: This R code helps researchers and practitioners calculate the value of financial options using the CRR Binomial Model
##Link to published paper: A Tutorial for Modeling Real Options Lattices from Project Cash Flows
##Link to code: https://doi.org/10.5281/zenodo.3885925
##Last update: June 9th, 2020

##Package needed for this code
if (!require(fOptions)) { 
  install.packages("fOptions") #We will use this package to calculate the values of financial options and to plot the lattices
}

##Parameters - Here, you can change the input values to suit the financial options you want to calculate  
n <- 1.5 #Time to maturity in years, which is equivalent to 18 months
i <- 9 #Number of time steps. Here, we would like to have a dt = 2 months. In this sense, we choose a number of intervals equal to 9.
vol <- 0.30 #Price volatility 
P0 <- 100 #Price at t = 0
r <- 0.06 #Risk free rate
C<- 120 #Call exercise price
P<- 90 #Put exercise price

##CRR Binomial Model
#Call Option
A<-BinomialTreeOption(TypeFlag = "ca", S = P0, X = C,
                      Time = n, r = r, b = r, sigma = vol, n = i)
A[1,1] #At the starting step of this lattice, we can verify that the value of the call option is: $11.00
#Saving this lattice in a pdf file 
pdf(file = "CRR Binomial Model (call).pdf", #You can name the file and change the directory you want to save it
    width = 13.00, #You can choose the width of the plot in inches
    height = 10.00) #and the height of the plot in inches
BinomialTreePlot(A, dy = 1, cex = 0.8, axes = FALSE, ylim = c(-15, 15),
                 xlab = " ", ylab = " ")
title(main = "CRR Binomial Model (call)")
dev.off()

#Put Option
B<-BinomialTreeOption(TypeFlag = "pa", S = P0, X = P,
                      Time = n, r = r, b = r, sigma = vol, n = i)
B[1,1] #At the starting step of this lattice, we can verify that the value of the put option is: $6.71
#Saving this lattice in a pdf file 
pdf(file = "CRR Binomial Model (put).pdf", #You can name the file and change the directory you want to save it
    width = 13.00, #You can choose the width of the plot in inches
    height = 10.00) #and the height of the plot in inches
BinomialTreePlot(B, dy = 1, cex = 0.8, axes = FALSE, ylim = c(-15, 15),
                 xlab = " ", ylab = " ")
title(main = "CRR Binomial Model (put)")
dev.off()